<?php

namespace App\Http\Controllers\Home;

use App\Models\Appointment;
use App\Models\Card;
use App\Models\Charge;
use App\Models\Setting;
use App\Models\User;
use App\Support\Wechat\Payment\Notify;
use Omnipay\Omnipay;

class NotifyController extends BaseController
{
    private $notify;

    public function __construct(User $user, Setting $setting, Appointment $appointment, Card $card, Charge $charge)
    {
        $wechatmch = $setting->getSetting('wechat');
        $this->notify = new Notify(
            $wechatmch['app_id'],
            $wechatmch['app_secret'],
            $wechatmch['mch_id'],
            $wechatmch['mch_key']
        );
        $this->appointment = $appointment;
        $this->card = $card;
        $this->charge = $charge;
        $this->union = $setting->getSetting('union');
    }

    /**
     * 微信支付成功返回信息
     */
    public function anyPayNotify()
    {
        $transaction = $this->notify->verify();
        if (!$transaction) {
//             tolog('挂号支付失败，时间：'.date('Y-m-d H:i:s',time()));
            $this->notify->reply('FAIL', 'verify transaction error');
        } else {
            if ($transaction['return_code'] == "SUCCESS") {
                $where['order_no'] = $transaction['out_trade_no'];
                $appointment = $this->appointment->getOne($where);
                if (!$appointment['status']) {
                    $res = $this->appointment->dealOrder($appointment);
                    if (!$res) {
//                         tolog('挂号支付成功，但更新订单状态失败，订单号：'.$transaction['out_trade_no']);
                    } else {
//                         tolog('挂号支付成功');
                    }
                }
            }
        }
        echo $this->notify->reply();
    }

    /**
     * 微信支付成功返回信息
     */
    public function anyChargeNotify()
    {
        $transaction = $this->notify->verify();
        if (!$transaction) {
//            tolog('充值支付失败，时间：'.date('Y-m-d H:i:s',time()));
            $this->notify->reply('FAIL', 'verify transaction error');
        } else {
            if ($transaction['return_code'] == "SUCCESS") {
                $where['order_no'] = $transaction['out_trade_no'];
                $charge = $this->charge->getOne($where);
                if (!$charge['status']) {
                    $res = $this->charge->dealOrder($charge, $transaction['transaction_id']);
                    if (!$res) {
//                        tolog('充值支付成功，但更新订单状态失败，订单号：'.$transaction['out_trade_no']);
                    }
                }
            }
        }
        echo $this->notify->reply();
    }

    public function anyUnionPayNotify()
    {
        $gateway = Omnipay::create('UnionPay_Express');
        $gateway->setMerId($this->union['mer_id']);
        $gateway->setCertDir(storage_path('unionpay/certs')); //The directory contain *.cer files
        $response = $gateway->completePurchase(['request_params' => $_REQUEST])->send();
        if ($response->isPaid()) {
            //pay success
            $data = $response->getData();
            $where['order_no'] = $data['orderId'];
            $appointment = $this->appointment->getOne($where);
            if (!$appointment['status']) {
                $res = $this->appointment->dealOrder($appointment);
                if (!$res) {
//                         tolog('挂号支付成功，但更新订单状态失败，订单号：'.$transaction['out_trade_no']);
                } else {
//                         tolog('挂号支付成功');
                }
            }
        } else {
            //pay fail
        }
    }

    public function anyUnionChargeNotify()
    {
        $gateway = Omnipay::create('UnionPay_Express');
        $gateway->setMerId($this->union['mer_id']);
        $gateway->setCertDir(storage_path('unionpay/certs')); //The directory contain *.cer files
        $response = $gateway->completePurchase(['request_params' => $_REQUEST])->send();
        if ($response->isPaid()) {
            //pay success
            $data = $response->getData();
            $where['order_no'] = $data['orderId'];
            $charge = $this->charge->getOne($where);
            if (!$charge['status']) {
                $res = $this->charge->dealOrder($charge, $data['transaction_id']);

                // 调接口给卡号充值
                $param['id'] = $charge['id'];
                $param['wxbillno'] = date('Y-m-d H:i:s', time());

                if (!$res) {
//                        tolog('充值支付成功，但更新订单状态失败，订单号：'.$transaction['out_trade_no']);
                }
            }
        }

    }
}


